;(function($){
	$.spa_front_account_type = $.spa_front_account_type || {}
	$.fn.spa_front_account_type = function(opts){
		var el = this;
		var opt = {
			no : "1883",//餐饮默认
			height : 110,
			height1 : 136,
			width : "",
			allNum : 25,
			twidthNum : 6,
			allPage : 1,
			page : 1,
			arg : "new",
			bh : "",//选中编号
			turnBtnPercent : 0.05,
			xNum : 5,
			yNum : 6,
			type : "code",//code : 助记码;type : 菜品大类;
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		param.width = el.width();
		var app = new App(el,param);
		$.spa_front_account_type = $.extend($.spa_front_account_type,app);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.getGoodsCbDs();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//菜单点击事件
			el.find(".dishes_type_each").unbind("click").click(function(){
				el.find(".btn-primary").removeClass("currentType").removeClass("btn-primary").addClass("btn-light");
				$(this).addClass("currentType").addClass("btn-primary").removeClass("btn-light");
				var json = {bh:$(this).data("bh")};
				param.bh = $(this).data("bh");
				param.clickBack(json);
			})
			
			//翻页按钮
			el.find(".dishes_btn").unbind("click").click(function(){
				var direction = $(this).data("direction");
				if(direction=="menu"){
					if(!$.isFunction($().posorder_dish_type)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/cashier_mana/jquery.plugin.posorder_dish_type.js");
			            $.ajaxSetup({ async: true });
					}
					el.find("#modal-plugin").modal("show");
					el.find("#modal-plugin").posorder_dish_type({
						ct_cb : param.ct_cb,
						bh : param.bh,
						dataBack : function(js){
							var index = js.index;
							param.bh = js.bh;
							param.page = Math.ceil(index/(param.twidthNum*2));
							var json = {bh:js.bh};
							param.clickBack(json);
							setTimeout(function(){
								t.initGoodsCbHtml();
							},100)
						}
					})
				}else if(direction=="up"){
					param.page = param.page-1;
					t.initGoodsCbHtml();
				}else if(direction=="down"){
					param.page = param.page+1;
					t.initGoodsCbHtml();
				}
			});
			
		},
		initGoodsCbHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var ct_cb = param.ct_cb 
			
			param.allPage =  Math.ceil(ct_cb.length/(param.twidthNum*2))
			
			el.addClass("flex");
			
			var ct_cb = param.ct_cb; 
			
			el.css("height",param.height);
			var turnBtnWidth = param.turnBtnPercent*param.width;
			var cbWidth = param.width - turnBtnWidth;
			var html = '<div id="modal-plugin" class="modal"></div><div class="type_each" style="width:'+cbWidth+'px;display:table;">';
			
			var direction = "menu";
			var upHide = "hide"; 
			var menuHide = "";
			var downDisabled = "";
			if(param.page>1){
				direction = "up";
				var upHide = ""; 
				var menuHide = "hide";
			}
			if(param.allPage==param.page){
				downDisabled = "disabled";
			}
			var btnHtml = 	'<div style="width:'+turnBtnWidth+'px;display:table;">'+
								'<button data-page="'+param.page+'" style="display:table-cell;width:'+turnBtnWidth+'px; !important;" class="btn btn-app btn-purple btn-sm dishes_btn_up dishes_btn" data-direction="'+direction+'">'+
									'<i class="ace-icon fa fa-angle-up bigger-200 '+upHide+'"></i>'+
									'<i class="ace-icon glyphicon glyphicon-option-horizontal bigger-200 '+menuHide+'"></i>'+
								'</button>'+
								'<button style="display:table-cell;width:'+turnBtnWidth+'px !important;" class="btn btn-app btn-purple btn-sm dishes_btn_down dishes_btn" data-direction="down" '+downDisabled+'>'+
									'<i class="ace-icon fa fa-angle-down bigger-200"></i>'+
								'</button>'+
							'</div>';
			
			var cbHtml = '';
			var upper = param.page*param.twidthNum*2
			var lower = (param.page-1)*param.twidthNum*2
			$(ct_cb).each(function(k,v){
				var marginValue = 1;
				var diff = 2 - marginValue
				if(k>=lower&&k<upper){
					var thtml=getTypeElement(v,param.bh,cbWidth+12*diff,param.height,param.twidthNum,k)
					cbHtml+=thtml
				}
			});
			
			html = html + cbHtml + '</div>' + btnHtml ;
			el.html(html);
			el.find(".btn.btn-app").css("height",$(".dishes_type_each").height());
			t.bindEvent();
			if(param.bh==""){
				el.find(".dishes_type_each").eq(0).trigger("click");
			}else{
				el.find(".dishes_type_each[data-bh="+param.bh+"]").eq(0).trigger("click");
			}
		},
		getGoodsCbDs : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
//			获取菜别数据源
			var ds = $.parseJSON(localStorage.getItem("hodict"));
			var filter = "select * from json where (grou_name=='#菜别')";
			param.ct_cb = jsonsql.query( filter,ds);
			$(param.ct_cb).each(function(k,v){
				v.bh = v.code;
				v.mc = v.descript1;
			});
			t.initGoodsCbHtml();
		},
		initCodeHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			el.removeClass("flex");
			var html=	'<button type="button" class="btn btn-app btn-light A">A</button>'+
						'<button type="button" class="btn btn-app btn-light B">B</button>'+
						'<button type="button" class="btn btn-app btn-light C">C</button>'+
						'<button type="button" class="btn btn-app btn-light D">D</button>'+
						'<button type="button" class="btn btn-app btn-light E">E</button>'+
						'<button type="button" class="btn btn-app btn-light F">F</button>'+
						'<button type="button" class="btn btn-app btn-light G">G</button>'+
						'<button type="button" class="btn btn-app btn-light H">H</button>'+
						'<button type="button" class="btn btn-app btn-light I">I</button>'+
						'<button type="button" class="btn btn-app btn-light J">J</button>'+
						'<button type="button" class="btn btn-app btn-light K">K</button>'+
						'<button type="button" class="btn btn-app btn-light L">L</button>'+
						'<button type="button" class="btn btn-app btn-light M">M</button>'+
						'<button type="button" class="btn btn-app btn-light N">N</button>'+
						'<button type="button" class="btn btn-app btn-light O">O</button>'+
						'<button type="button" class="btn btn-app btn-light P">P</button>'+
						'<button type="button" class="btn btn-app btn-light Q">Q</button>'+
						'<button type="button" class="btn btn-app btn-light R">R</button>'+
						'<button type="button" class="btn btn-app btn-light S">S</button>'+
						'<button type="button" class="btn btn-app btn-light T">T</button>'+
						'<button type="button" class="btn btn-app btn-light W">W</button>'+
						'<button type="button" class="btn btn-app btn-light X">X</button>'+
						'<button type="button" class="btn btn-app btn-light Y">Y</button>'+
						'<button type="button" class="btn btn-app btn-light Z">Z</button>'+
						'<button type="button" class="btn btn-app btn-light 1">1</button>'+
						'<button type="button" class="btn btn-app btn-light 2">2</button>'+
						'<button type="button" class="btn btn-app btn-light 3">3</button>'+
						'<button type="button" class="btn btn-app btn-light 4">4</button>'+
						'<button type="button" class="btn btn-app btn-light 5">5</button>'+
						'<button type="button" class="btn btn-app btn-light 6">6</button>'+
						'<button type="button" class="btn btn-app btn-light 7">7</button>'+
						'<button type="button" class="btn btn-app btn-light 8">8</button>'+
						'<button type="button" class="btn btn-app btn-light 9">9</button>'+
						'<button type="button" class="btn btn-app btn-light 0">0</button>'+
						'<button type="button" class="btn btn-app btn-light goclear">'+$.i18n.propJsp("清除")+'</button>'+
						'<button type="button" class="btn btn-app btn-light goback">'+$.i18n.propJsp("回退")+'</button>';
			el.html(html);
			var left = el.offset().left+25
			var html = '<div class="nav-search" data-posorder="keyboard" id="nav-search" style="width:300px;z-index:1000;position:absolute;top:9px;left:'+left+'px;">'+
							'<form class="form-search" action="javascript:void();">'+
								'<span class="input-icon">'+
									'<input type="text" placeholder="ALT+S" class="nav-search-input" id="nav-search-input" autocomplete="off">'+
									'<i class="ace-icon fa fa-search nav-search-icon"></i>'+
								'</span>'+
							'</form>'+
						'</div>';
			var size = $("body").find("#nav-search[data-posorder=keyboard]").size()
			if(size>0){
				$("body").find("#nav-search[data-posorder=keyboard]").remove();
			}
			$("body").append(html);
			t.getDefault()
			t.bindClick()
		},
		getDefault : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var marginValue = 1
    		var padding = 5
    		if($(window).width() < 1025){
    			$("#goodsCb .btn.btn-app").css("width",el.width()/12-2*marginValue)
    			$(".nav-search .nav-search-input").width($("#goodsCb .btn.btn-app").width()*2-padding+1)
    		}else{
    			$("#goodsCb .btn.btn-app").css("width",el.width()/12-2*marginValue)
    			$(".nav-search .nav-search-input").width($("#goodsCb .btn.btn-app").width()*2-padding+1)
    		}
		},
		bindClick : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//助记码输入
			$("#goodsCb button").click(function(){
				if($(this).hasClass("goback")){
					var code = $("#nav-search-input").val();
					code = code.substring(0,code.length-1);
					$("#nav-search-input").val(code);
				}else if($(this).hasClass("goclear")){
					$("#nav-search-input").val("");
					$("#posorderDiv .pd_info").html("");
					$("#posorderDiv .pd_scroll").hide();
				}else{
					var code = $("#nav-search-input").val();
					code = code + $(this).text();
					$("#nav-search-input").val(code);
					$(".goback,.goclear").removeClass("btn-light").addClass("btn-primary");
				}
				if(!$(this).hasClass("goclear") && !!code){
					pym = code;
					cb_bh = "";
					if(param.arg){
						param.pymBack(pym);
						return;
					}
					$("#nav-search-input").codeSearch({
						pym:pym?pym:"",//助记码
						cb_bh:cb_bh,//菜别
						page:page?page:1,//页数
						rows:allNum?allNum:1,//显示的菜数
						//初始化数据返回回调
						getDefaultBack:function(data){
							doView.init(data);
						}
					});		
				}else{
					$("#posorderDiv .pd_info").html("");
					$("#posorderDiv .pd_scroll").hide();
					$(".goback,.goclear").removeClass("btn-primary").addClass("btn-light");
				}
			});
			
			jQuery(document).unbind('keyup').bind('keyup', 'Alt+s', function (evt){
				if(evt.keyCode==18){
					$("#nav-search[data-posorder=keyboard]").find("#nav-search-input").focus().select();
					return false;
				}
			});
			
			$("#nav-search[data-posorder=keyboard]").find("#nav-search-input").unbind("keydown").keydown(function(e){
				var value = this.value
				if(e.keyCode==13){
					param.clickBack(value);
				}
			})
			
			$("#nav-search[data-posorder=keyboard]").find("#nav-search-input").unbind('keyup').keyup(function(e){
				var value = this.value.toLocaleUpperCase();
				$(this).val(value);
				param.clickBack(value);
				$("#nav-search[data-posorder=keyboard]").find("#nav-search-input").focus();
			})
			
			$("#nav-search[data-posorder=keyboard]").find("#nav-search-input").unbind('change').change(function(e){
				var value = this.value.toLocaleUpperCase();
				$(this).val(value);
			})
		},
	}
	
	//获取菜品分类对象
	function getTypeElement(data,bh,width,height,twidthNum,index){
		var $type;
		var thisclass="btn-light"//默认样式
		if(data.bh==bh){
			thisclass="btn-primary"//选中样式
		}else{
			thisclass="btn-light"//默认样式
		}
		var fontsize=18//默认字体大小
		var width=width/twidthNum-6//菜别类型宽度
		var eachlength=data.mc.length//名称长度
		var maxinclude=0//最大容量
		var ifsmaller=""
		maxinclude=width/fontsize
		if($(window).width()>1024){
			fontsize=18
		}else{
			fontsize=15
		}
		if(eachlength>=maxinclude){//超出容量
			fontsize=width/eachlength
			ifsmaller=" besmall"
		}
		$type=$('<div><button type="button" class="btn btn-app '+thisclass+' dishes_type_each" data-bh="'+ data.bh +'" data-index="'+index+'">'+
						'<div class="type_content'+ifsmaller+'">'+ data.mc +'</div>'+
					'</button></div>')
		$type.find(".dishes_type_each").css("font-size",fontsize+"px")//设置字体大小
		$type.find(".dishes_type_each").css("width",width).css("min-width",width)//设置宽度
		var hhhh = (height/2-6)>50 ? 50 : ((height/2-6)<30 ? 30 : (height/2-6))
//		$type.find(".dishes_type_each").css("height",height/2-6).css("max-height",height/2-6)//设置高度
		$type.find(".dishes_type_each").css("height",hhhh)
		$type.find(".dishes_type_each").css("padding",0)
		$type.find(".dishes_type_each").css("text-align","center")
		$type.find(".type_content").css("line-height",hhhh+"px")
		$type.find(".type_content").css("width","100%").css("height","100%")
		return $type.html()
	}

	//补全小数位
	function toFloot(num){//1.00,1.10,1.01,1,1.1
		num=num.toString()
		if(num.indexOf(".00")==-1){//非标准格式
			if(num.indexOf(".")==-1){//整数
				num=num+".00"
			}else{//小数
				var floot=num.split(".")[1]//小数值
				if(floot.length==1){//存在一位小数
					num=num+"0"
				}else if(floot.length==2){//存在两位小数
					num=num
				}else{
					num=num.split(".")[0]+"."+floot.substring(0,2)
				}
			}
		}else{
			var floot=num.split(".")[1]//小数值
			num=num.split(".")[0]+"."+floot.substring(0,2)
		}
		return num
	}
	
})(jQuery);